#include "chgDsgPolicy.h"
#include "SystemParam.h"
#include <rtthread.h>
#include <stdio.h>
#include <stdlib.h>
//温度对应的充电需求电流，，共10个区间,可根据实际需要进行调整
typedef struct{
	int8_t 		temp;//单位：0.1A
	uint16_t 	chgReqCurr;//单位：0.1A
}ChgTempCur;

static ChgTempCur chgTempPolicy[10] = {
	{.temp = -30,.chgReqCurr = 5}, //temp0<temp<=temp1, 电流单位:0.1A
	{.temp = -20,.chgReqCurr = 5}, //temp1<temp<=temp2, 电流单位:0.1A
	{.temp = -10,.chgReqCurr = 10},//temp2<temp<=temp3, 电流单位:0.1A
	{.temp = 0,  .chgReqCurr = 20},//temp3<temp<=temp4, 电流单位:0.1A
	{.temp = 10, .chgReqCurr = 30},//temp4<temp<=temp5, 电流单位:0.1A
	{.temp = 20, .chgReqCurr = 50},//temp5<temp<=temp6, 电流单位:0.1A
	{.temp = 40, .chgReqCurr = 45},//temp6<temp<=temp7, 电流单位:0.1A
	{.temp = 50, .chgReqCurr = 30},//temp7<temp<=temp8, 电流单位:0.1A
	{.temp = 55, .chgReqCurr = 20},//temp8<temp<=temp9, 电流单位:0.1A
	{.temp = 60, .chgReqCurr = 10}  //temp9<temp<temp10, 电流单位:0.1A
};

static int8_t  batTempOne = 0;//电池温度1
static int8_t  batTempTwo = 0;//电池温度2

static uint8_t batTempOneArea = 0;
static uint8_t batTempTwoArea = 0;

uint8_t getTempCtrlPolicyLow(void){
	return chgTempPolicy[0].temp;
}

uint8_t getTempCtrlPolicyHigh(void){
	return chgTempPolicy[9].temp;
}

static void tempCtrlChgPolicy(void){
	
	if(batTempOne <= chgTempPolicy[0].temp||batTempOne >= chgTempPolicy[9].temp){//如果温度1处于区间之外
		getRealTimeInfo()->batChgReqCurrent = 0;
	}else if(batTempTwo <= chgTempPolicy[0].temp||batTempTwo >= chgTempPolicy[9].temp){//如果温度2处于区间之外
		getRealTimeInfo()->batChgReqCurrent = 0;
	}else{//两个温度都在区间之内
		if(abs(batTempOne - getRealTimeInfo()->batTempOne) >= 2){
			batTempOne = getRealTimeInfo()->batTempOne;
			uint8_t i = 0;
			for(i=0;i<10;i++){
				if(batTempOne > chgTempPolicy[i].temp){
					batTempOneArea ++;
				}
			}
		}
		if(abs(batTempTwo - getRealTimeInfo()->batTempTwo) >= 2){
			batTempTwo = getRealTimeInfo()->batTempTwo;
			uint8_t i = 0;
			for(i=0;i<10;i++){
				if(batTempTwo > chgTempPolicy[i].temp){
					batTempTwoArea ++;
				}
			}
		}
		if(batTempOneArea >= 10){
			batTempOneArea = 10;
		}
		if(batTempTwoArea >= 10){
			batTempTwoArea = 10;
		}
		if(chgTempPolicy[batTempOneArea].chgReqCurr < chgTempPolicy[batTempTwoArea-1].chgReqCurr){
			getRealTimeInfo()->batChgReqCurrent = chgTempPolicy[batTempOneArea-1].chgReqCurr;
		}else{
			getRealTimeInfo()->batChgReqCurrent = chgTempPolicy[batTempTwoArea-1].chgReqCurr;
		}
	}
//	printf("batTempOneArea = %d\n",batTempOneArea);
//	printf("batTempTwoArea = %d\n\n",batTempTwoArea);
	getRealTimeInfo()->batDsgLimitCurrent = getSysSettings()->protectParam.dsgOverCurrent*10 - 5;//放电限制电流比保护电流小0.5A
}

static void paramCtrlChgPolicy(void){
	int8_t tempOne = getRealTimeInfo()->batTempOne;
	int8_t tempTwo = getRealTimeInfo()->batTempTwo;
	int8_t chgTempLow = getSysSettings()->protectParam.chgTempLow;
	int8_t chgTempHigh = getSysSettings()->protectParam.chgTempHigh;
	if(tempOne<=chgTempLow||tempOne>=chgTempHigh||tempOne<=chgTempLow||tempOne>=chgTempHigh){
		getRealTimeInfo()->batChgReqCurrent = 0;//充电低温或充电过温，充电需求电流为0
	}else{
		getRealTimeInfo()->batChgReqCurrent = getSysSettings()->protectParam.chgOverCurrent*10 - 5;//充电需求电流比保护电流小0.5A
	}
	
	getRealTimeInfo()->batDsgLimitCurrent = getSysSettings()->protectParam.dsgOverCurrent*10 - 5;//放电限制电流比保护电流小0.5A
}

void chg_dsg_policy_update(void){
	
	switch(getSysSettings()->factoryParam.chargeMap){
		case paramCtrlPolicy:{
			paramCtrlChgPolicy();
		}break;
		case tempCtrlPolicy:{
			tempCtrlChgPolicy();
		}break;
	}
}